import request from '../../utils/webPageRequest'
import PageCI from '../../model/pageCI'
// import ApiError from '../../error/ApiError'
// import ApiErrorNames from '../../error/ApiErrorNames'
class WebProductContorller {
    static async webpagehttp(ctx) {
        try {
            // console.log('ctx.request.body', typeof ctx.request.body)
            console.log('ctx', ctx.header)
            let reqjson = ctx.request.body
            try {
                reqjson = JSON.parse(ctx.request.body)

            } catch (error) {
                // console.log('error', error)
            }
            const { hostname = '', uuid = '' } = ctx.header
            if (hostname && uuid) {
                const pcidata = await PageCI.findOne({ uuid })
                if (pcidata) {
                    // console.log('pcidata', pcidata)
                    const getIsAdd = pcidata.pageInfoArr.find(item => {
                        return item.hostname === hostname
                    })
                    // console.log('getIsAdd', getIsAdd)
                    if (getIsAdd) {
                        ctx.header.t = getIsAdd.cookie
                        const gethttpData = await request({
                            url: reqjson.url,
                            method: reqjson.method,
                            ctx
                        }).catch(err => {
                            console.log('err', err)
                            ctx.body = err
                        })
                        // console.log('gethttpData', gethttpData)
                        ctx.body = gethttpData
                    } else {
                        // throw new ApiError(ApiErrorNames.NogetCooke)
                        ctx.body = {
                            code: 4037,
                            msg: '没有获取到应用平台cookie'
                        }

                    }
                    // console.log('getIsAdd', getIsAdd)
                } else {
                    ctx.body = {
                        code: 4037,
                        msg: '没有获取到应用平台cookie'
                    }
                }
                return
            }
            // console.log('reqjson', reqjson)
            const gethttpData = await request({
                url: reqjson.url,
                method: reqjson.method,
                ctx
            }).catch(err => {
                // console.log('err', err)
                ctx.body = err
            })
            console.log('gethttpData', gethttpData)
            ctx.body = gethttpData
        } catch (error) {
            console.log('error', error)
            throw new Error('请求失败')
        }
        // console.log('请求再请求', ctx.request.body )
    }
}

export default WebProductContorller
